package com.lujianfei.imagesearch.presenter;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import com.lujianfei.imagesearch.Constants;
import com.lujianfei.imagesearch.contract.MainContract;
import com.lujianfei.imagesearch.contract.MainContract.View;
import com.lujianfei.imagesearch.data.SPRepository;

public class MainPresenter extends MainContract.Presenter {

	String absolutePath;
	SPRepository mSPRepository = new SPRepository();

	public MainPresenter(View mView) {
		super(mView);
	}

	@Override
	public void openFile(int type) {
		switch (type) {
		case Constants.SUBFrameType.IMAGE:
		{
			// 打开文件选择窗口
			JFileChooser chooser = new JFileChooser();
			chooser.setCurrentDirectory(new File(mSPRepository.getImagePath()));
			chooser.setFileFilter(new FileFilter() {

				@Override
				public String getDescription() {
					// TODO Auto-generated method stub
						return "Image *.jpg or *.png";
				}

				@Override
				public boolean accept(File f) {
					// TODO Auto-generated method stub
					String absolutePath = f.getAbsolutePath();
					return absolutePath.endsWith("jpg") || absolutePath.endsWith("png");
				}
			});
			int showOpenDialog = chooser.showOpenDialog(mView.getContext());
			if (JFileChooser.APPROVE_OPTION == showOpenDialog) {
				File selectedFile = chooser.getSelectedFile();
				absolutePath = selectedFile.getAbsolutePath();
				mView.setImage(absolutePath,type);
				mSPRepository.setImagePath(absolutePath);
			}
		}
			break;
		case Constants.SUBFrameType.MNIST_DATA:
		{
			// 打开文件选择窗口
			JFileChooser chooser = new JFileChooser();
			chooser.setCurrentDirectory(new File(mSPRepository.getMnistDataPath()));
			chooser.setFileFilter(new FileFilter() {

				@Override
				public String getDescription() {
					// TODO Auto-generated method stub
						return "Mnist Data (*ubyte)";
				}

				@Override
				public boolean accept(File f) {
					// TODO Auto-generated method stub
					String absolutePath = f.getAbsolutePath();
					return absolutePath.endsWith("ubyte");
				}
			});
			int showOpenDialog = chooser.showOpenDialog(mView.getContext());
			if (JFileChooser.APPROVE_OPTION == showOpenDialog) {
				File selectedFile = chooser.getSelectedFile();
				absolutePath = selectedFile.getAbsolutePath();
				mView.setImage(absolutePath,type);
				mSPRepository.setMnistDataPath(absolutePath);
			}
		}
		break;
		case Constants.SUBFrameType.MNIST_LABEL:
		{
			// 打开文件选择窗口
			JFileChooser chooser = new JFileChooser();
			chooser.setCurrentDirectory(new File(mSPRepository.getMnistLabelPath()));
			chooser.setFileFilter(new FileFilter() {

				@Override
				public String getDescription() {
					// TODO Auto-generated method stub
						return "Mnist Label (*ubyte)";
				}

				@Override
				public boolean accept(File f) {
					// TODO Auto-generated method stub
					String absolutePath = f.getAbsolutePath();
					return absolutePath.endsWith("ubyte");
				}
			});
			int showOpenDialog = chooser.showOpenDialog(mView.getContext());
			if (JFileChooser.APPROVE_OPTION == showOpenDialog) {
				File selectedFile = chooser.getSelectedFile();
				absolutePath = selectedFile.getAbsolutePath();
				mView.setImage(absolutePath,type);
				mSPRepository.setMnistLabelPath(absolutePath);
			}
		}
		break;

		default:
			break;
		}
	}


	void log(String text) {
		System.out.println("MainPresenter:" + text);
	}
}
